package com.youhome.housing.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.CustomCompanyTokenDetails;
import com.youhome.service.service.ListHubPhotoService;
import com.youhome.service.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2019/03/22.
 */
@RestController
@Api(tags = "图片", position = 3)
@RequestMapping(value = "/photo")
public class PhotoController {

    @Autowired
    private UploadService uploadService;
    @Autowired
    private ListHubPhotoService listHubPhotoService;

    @ApiOperation(value = "上传图片", notes = "上传图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/upload")
    public Object upload(@RequestParam("photo") MultipartFile photo) throws CustomException {
        return new Result(ResultCode.SUCCESS, uploadService.upload(photo, 0));
    }

    @ApiOperation(value = "删除图片", notes = "删除图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "photoUrl", value = "图片Url", required = true, paramType = "query")
    })
    @DeleteMapping(value = "/{houseId}")
    public Object delete(@PathVariable String houseId, String photoUrl,
                         @ApiIgnore @AuthenticationPrincipal CustomCompanyTokenDetails details) throws CustomException {
        listHubPhotoService.delete(houseId, photoUrl, details);
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "获取图片", notes = "获取图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    @GetMapping(value = "/{houseId}")
    public Object list(@PathVariable String houseId) {
        return new Result<>(ResultCode.SUCCESS, listHubPhotoService.selectPhotoByHouseId(houseId));
    }
}
